package com.business.serviceimpl;

import com.business.bean.Role;
import com.business.bean.RoleUser;
import com.business.bean.User;
import com.business.bean.layuiBean.ResultPage;
import com.business.bean.layuiBean.VoPage;
import com.business.mapper.UserMapper;
import com.business.service.RoleUserService;
import com.business.service.UserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.business.util.cacheUtil.JedisUtil;
import com.util.Respons.ResponseUtil;
import com.util.exception.CommonException;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ThreadContext;
import org.apache.shiro.web.servlet.ShiroHttpServletRequest;
import org.apache.shiro.web.servlet.ShiroHttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 张子艺
 * @since 2019-01-08
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
   @Autowired
    RoleUserService roleUserService;
    @Autowired
    JedisUtil jedisUtil;


    @Override
    public User login(String userName) {
        return baseMapper.login(userName);
    }

    @Override
    public Map<String, Object> loginGo(String userName, String passWord) {
        //添加用户认证信息
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(userName, passWord);
        try {
            //进行验证，这里可以捕获异常，然后返回对应信息
            subject.login(token);
        } catch (Exception e) {
           return ResponseUtil.getNoUser();
        }
        return ResponseUtil.getSuccessMap();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> delUser(Long userId, String userName, ShiroHttpSession shiroHttpSession) {
        if(this.deleteById(userId)){
            Map map=new HashMap();

            map.put("user_id",userId);
           roleUserService.deleteByMap(map);
           //清楚 session共享缓存
            jedisUtil.delSessionCache(userName);
        }
        return  ResponseUtil.getSuccessMap();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> insertUser(User user,Long roleId) {
        user.setCreateTime(System.currentTimeMillis());
         if(this.insert(user)){
             RoleUser roleUser=new RoleUser();
             roleUser.setRoleId(roleId);
             roleUser.setUserId(user.getId());
             if(roleUserService.insert(roleUser)){
                 return ResponseUtil.getSuccessMap();
             }
         }
        throw  new CommonException("新增失败");
    }

    @Override
    public ResultPage<User> findUserByPage(VoPage voPage) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1) * voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        List<User> list = baseMapper.selectMyPage(paramMap);
        Integer count = baseMapper.countMyPage(paramMap);
        ResultPage<User> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }
}
